package com.gavindon.android.view.video

import android.os.Bundle
import com.gavindon.android.R
import com.gavindon.android.base.BaseActivity
import com.gavindon.android.base.INTENT_COURSE_ID
import com.gavindon.android.base.INTENT_COURSE_NAME
import com.gavindon.android.network.DataResult
import com.gavindon.android.viewmodel.CommonViewModel
import com.gavindon.android.viewmodel.VideoViewModel
import com.orhanobut.logger.Logger
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.player.PlayerFactory
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import org.jetbrains.anko.toast
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager

/**
 * description: 播放视频
 * Created by liNan on  2022-2-6 12:04
 */
class VideoActivity : BaseActivity() {

    private val videoPlayer by lazy {
        findViewById<StandardGSYVideoPlayer>(R.id.detail_player)
    }

    private lateinit var mCommonViewModel: CommonViewModel

    override val layoutId = R.layout.activity_video

    private var courseId: String? = ""
    private var courseName: String? = ""

    private var videoPlayPosition = 0
    private var videoPlayPercent = 0

    private var lessonId: String? = null
    private lateinit var mViewModel: VideoViewModel
    override fun onInit(savedInstanceState: Bundle?) {
        courseId = intent.getStringExtra(INTENT_COURSE_ID)
        courseName = intent.getStringExtra(INTENT_COURSE_NAME)
        init()
    }

    private fun init() {
        mViewModel = getViewModel()
        mCommonViewModel = getViewModel()
        mViewModel.getCourseTree(courseId!!).observe(this, {
            if (it is DataResult.Success) {
                mViewModel.getLessonDetail(it.data[0].linkObjId).observe(this, { detail ->
                    if (detail is DataResult.Success) {
                        //用于记录观看时长
                        lessonId = detail.data.lessonId
                        videoPlayer.setUp(detail.data.fileUrl, true, courseName)
                        videoPlayer.startPlayLogic()
                    }
                })
            }
        })

        videoPlayer.setGSYVideoProgressListener { progress, secProgress, currentPosition, duration ->
            videoPlayPosition = currentPosition
            videoPlayPercent = progress
        }
    }

    override fun onPause() {
        super.onPause()
        videoPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        videoPlayer.onVideoResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        GSYVideoManager.releaseAllVideos()
    }

    override fun onBackPressed() {
        //释放所有
        videoPlayer.setVideoAllCallBack(null)
        //保存视频进度 ms->s
        if (videoPlayPosition > 1000 && !lessonId.isNullOrEmpty()) {
            mCommonViewModel.pushVideoProgress(
                lessonId!!,
                videoPlayPosition
            )
        } else if (videoPlayPercent > 98) {
            mCommonViewModel.pushVideoProgress(
                lessonId!!,
                videoPlayer.duration
            )
        }

        super.onBackPressed()
    }
}